﻿' 
' AnyRip (C) 2011 Rick Winkler
'
' This file is part of AnyRip.
' 
' AnyRip is free software: you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation, either version 3 of the License, or
' (at your option) any later version.
'
' AnyRip is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
' GNU General Public License for more details.
'
' You should have received a copy of the GNU General Public License
' along with AnyRip.  If not, see <http://www.gnu.org/licenses/>.
'
Imports System.IO
Imports System.Xml
Imports System.Security.Cryptography

Module MyMovies

    ' Get Remaining Lookups
    Public Sub ShowRemainingLookups(ByVal sUserName As String, ByVal sPassword As String)

        ' Create a WebRequest to the remote site
        Dim _HttpWebRequest As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create("https://api.mymovies.dk/?command=GetRemainingLookups&username=" & sUserName & "&password=" & sPassword & "&locale=1033")
        Dim _HttpWebResponse As System.Net.HttpWebResponse = _HttpWebRequest.GetResponse()

        ' Check if the response is OK (status code 200)
        If _HttpWebResponse.StatusCode = System.Net.HttpStatusCode.OK Then

            ' Parse the contents from the response to a stream object
            Dim _Stream As System.IO.Stream = _HttpWebResponse.GetResponseStream()
            Dim _StreamReader As New System.IO.StreamReader(_Stream)
            Dim _Contents As String = _StreamReader.ReadToEnd()
            Dim _XmlDocument As New System.Xml.XmlDocument()

            ' Load conents info XML document
            _XmlDocument.LoadXml(_Contents)

            ' Select the parent node
            Dim _XmlNodeList As XmlNodeList = _XmlDocument.SelectNodes("/")

            ' Return Value or -1 for error
            If _XmlNodeList.Count = 1 Then

                Utils.WriteLine(_XmlNodeList.Item(0).InnerText & " remaining lookups.")

            Else

                Utils.WriteLine("Could not get remaining lookups.")

            End If

        Else

            Utils.WriteLine("Could not get remaining lookups.")

        End If

    End Sub

    ' Get the title of a disc base on DiscId
    Function GetDiscTitleByDiscIds(ByVal _UserName As String, ByVal _Password As String, ByVal _DiscId As String, ByVal _Country As String)

        ' Create a WebRequest to the remote site
        Dim _HttpWebRequest As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create("https://api.mymovies.dk/?command=SearchDiscTitleByDiscIds&username=" & _UserName & "&password=" & _Password & "&locale=1033&discid=" & _DiscId & "&country=" & _Country)
        Dim _HttpWebResponse As System.Net.HttpWebResponse = _HttpWebRequest.GetResponse()

        ' Check if the response is OK (status code 200)
        If _HttpWebResponse.StatusCode = System.Net.HttpStatusCode.OK Then

            ' Parse the contents from the response to a stream object
            Dim _Stream As System.IO.Stream = _HttpWebResponse.GetResponseStream()
            Dim _StreamReader As New System.IO.StreamReader(_Stream)
            Dim _Contents As String = _StreamReader.ReadToEnd()
            Dim _XmlDocument As New System.Xml.XmlDocument()

            ' Load conents info XML document
            _XmlDocument.LoadXml(_Contents)

            ' Check for results
            For Each _XmlElement As XmlElement In _XmlDocument.GetElementsByTagName("Responce")
                If _XmlElement.GetAttribute("status").ToString = "none" Then
                    Return Nothing
                End If
            Next

            ' Select the parent node
            For Each _XmlElement As XmlElement In _XmlDocument.GetElementsByTagName("Title")
                Return _XmlElement.GetAttribute("title").ToString
            Next

            Return Nothing

        Else

            Return Nothing

        End If

    End Function

    ' Get the title of a disc base on Title
    Function GetDiscTitleByDiscTitle(ByVal _UserName As String, ByVal _Password As String, ByVal _Drive As String, ByVal _Country As String)

        ' Create a WebRequest to the remote site
        Dim _HttpWebRequest As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create("https://api.mymovies.dk/?command=SearchDiscTitleByTitle&username=" & _UserName & "&password=" & _Password & "&locale=1033&title=" & Dir(_Drive, FileAttribute.Volume) & "&country=" & _Country)
        Dim _HttpWebResponse As System.Net.HttpWebResponse = _HttpWebRequest.GetResponse()

        ' Check if the response is OK (status code 200)
        If _HttpWebResponse.StatusCode = System.Net.HttpStatusCode.OK Then

            ' Parse the contents from the response to a stream object
            Dim _Stream As System.IO.Stream = _HttpWebResponse.GetResponseStream()
            Dim _StreamReader As New System.IO.StreamReader(_Stream)
            Dim _Contents As String = _StreamReader.ReadToEnd()
            Dim _XmlDocument As New System.Xml.XmlDocument()

            ' Load conents info XML document
            _XmlDocument.LoadXml(_Contents)

            ' Check for results
            For Each _XmlElement As XmlElement In _XmlDocument.GetElementsByTagName("Responce")
                If _XmlElement.GetAttribute("status").ToString = "none" Then
                    Return Nothing
                End If
            Next

            ' Select the parent node
            For Each _XmlElement As XmlElement In _XmlDocument.GetElementsByTagName("Title")
                Return _XmlElement.GetAttribute("title").ToString
            Next

            Return Nothing

        Else

            Return Nothing

        End If

    End Function

    Function GetBasicTitle(ByVal _Drive As String) As String
        Return Dir(_Drive, FileAttribute.Volume)
    End Function

    Function GetDiscId(ByVal strDrive As String) As String

        Dim buffer As Byte()
        Dim provider As New SHA1CryptoServiceProvider
        Dim inputStream As FileStream = Nothing

        If IsHDDVD(strDrive) Then

            If File.Exists((strDrive & "\AACS\VTKF000.AACS")) Then
                inputStream = New FileStream((strDrive & "\AACS\VTKF000.AACS"), FileMode.Open, FileAccess.Read, FileShare.Read, &H1000)
            ElseIf File.Exists((strDrive & "\AACS\VTKF.AACS")) Then
                inputStream = New FileStream((strDrive & "\AACS\VTKF.AACS"), FileMode.Open, FileAccess.Read, FileShare.Read, &H1000)
            ElseIf File.Exists((strDrive & "\SLY!\VTKF000.AACS")) Then
                inputStream = New FileStream((strDrive & "\SLY!\VTKF000.AACS"), FileMode.Open, FileAccess.Read, FileShare.Read, &H1000)
            ElseIf File.Exists((strDrive & "\ANY!\VTKF000.AACS")) Then
                inputStream = New FileStream((strDrive & "\ANY!\VTKF000.AACS"), FileMode.Open, FileAccess.Read, FileShare.Read, &H1000)
            ElseIf File.Exists((strDrive & "\!ANY\VTKF000.AACS")) Then
                inputStream = New FileStream((strDrive & "\!ANY\VTKF000.AACS"), FileMode.Open, FileAccess.Read, FileShare.Read, &H1000)
            ElseIf File.Exists((strDrive & "\!SLY\VTKF000.AACS")) Then
                inputStream = New FileStream((strDrive & "\!SLY\VTKF000.AACS"), FileMode.Open, FileAccess.Read, FileShare.Read, &H1000)
            ElseIf File.Exists((strDrive & "\ADV_OBJ\DISCID.DAT")) Then
                inputStream = New FileStream((strDrive & "\ADV_OBJ\DISCID.DAT"), FileMode.Open, FileAccess.Read, FileShare.Read, &H1000)
            End If

            Try
                buffer = provider.ComputeHash(inputStream)
            Finally
                inputStream.Dispose()
            End Try

            Return BitConverter.ToString(buffer).Replace("-", "")

        ElseIf IsBluRay(strDrive) Then

            If File.Exists((strDrive & "\AACS\Unit_Key_RO.inf")) Then
                inputStream = New FileStream((strDrive & "\AACS\Unit_Key_RO.inf"), FileMode.Open, FileAccess.Read, FileShare.Read, &H1000)
            ElseIf File.Exists((strDrive & "\SLY!\Unit_Key_RO.inf")) Then
                inputStream = New FileStream((strDrive & "\SLY!\Unit_Key_RO.inf"), FileMode.Open, FileAccess.Read, FileShare.Read, &H1000)
            ElseIf File.Exists((strDrive & "\ANY!\Unit_Key_RO.inf")) Then
                inputStream = New FileStream((strDrive & "\ANY!\Unit_Key_RO.inf"), FileMode.Open, FileAccess.Read, FileShare.Read, &H1000)
            ElseIf File.Exists((strDrive & "\!ANY\Unit_Key_RO.inf")) Then
                inputStream = New FileStream((strDrive & "\!ANY\Unit_Key_RO.inf"), FileMode.Open, FileAccess.Read, FileShare.Read, &H1000)
            ElseIf File.Exists((strDrive & "\!SLY\Unit_Key_RO.inf")) Then
                inputStream = New FileStream((strDrive & "\!SLY\Unit_Key_RO.inf"), FileMode.Open, FileAccess.Read, FileShare.Read, &H1000)
            End If

            Try
                buffer = provider.ComputeHash(inputStream)
            Finally
                inputStream.Dispose()
            End Try

            Dim _DiscId As String = BitConverter.ToString(buffer).Replace("-", "")

            Utils.WriteLine("Found DiscId " & _DiscId)

            Return BitConverter.ToString(buffer).Replace("-", "")

        ElseIf IsDVD(strDrive) = 0 Then

            ' Return GetDiscId(strDrive, "DVD")

        Else

            Utils.WriteLine("Could not determine disc type, exiting.")

        End If

        Return Nothing

    End Function

    ' Check if disc is a HD-DVD
    Private Function IsHDDVD(ByVal _Drive As String) As Boolean
        If Directory.Exists(_Drive & "\HVDVD_TS") Then
            Utils.WriteLine("Found HD-DVD disk in drive " & _Drive)
            Return True
        Else
            Return False
        End If
    End Function

    ' Check if disc is a BluRay
    Public Function IsBluRay(ByVal _Drive As String) As Boolean
        If Directory.Exists(_Drive & "\BDMV") Then
            Utils.WriteLine("Found Bluray disk in drive " & _Drive)
            Return True
        Else
            Return False
        End If
    End Function

    ' Check if disc is a DVD
    Function IsDVD(ByVal _Drive) As Boolean
        If File.Exists(_Drive & "\VIDEO_TS\VIDEO_TS.IFO") Then
            Utils.WriteLine("Found DVD disk in drive " & _Drive)
            Return True
        Else
            Return False
        End If
    End Function

End Module
